package com.example.blog.intercepter;

import com.example.blog.constants.Constant;
import com.example.blog.model.UserInfo;
import com.example.blog.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 1. 从请求中拿到 token
        String token = request.getHeader(Constant.USER_LOGIN_TOKEN);
        // 2. 校验 token
        Claims claims = JwtUtil.verifyJwt(token);
        if(claims == null){
            response.setStatus(401);
            return false;
        }
        return true;

//        // 1. 获取会话
//        HttpSession session = request.getSession();
//        // 2. 从会话中获取用户信息
//        UserInfo userInfo = (UserInfo) session.getAttribute(Constant.SESSION_USERINFO_KEY);
//        // 3. 判断用户是否登录
//        if(userInfo == null){
//            return false;
//        }
//        return true;
    }
}
